'use strict';
const dayjs = require('dayjs');
const fs = require('fs');

// eslint-disable-next-line no-unused-vars
module.exports = options => {

  return async (ctx, next) => {
    // console.log(options);
    const sTime = Date.now();
    const startTime = dayjs(Date.now()).format('YYYY-MM-DD HH:mm:ss');
    const { method, url, body } = ctx.request;
    await next();
    const log = {
      method, url, body,
      startTime,
      endTime: dayjs(Date.now()).format('YYYY-MM-DD HH:mm:ss'),
      timeLength: Date.now() - sTime + ' ms',
    };
    const baseDir = ctx.app.baseDir;
    const data = dayjs(Date.now()).format('YYYY-MM-DD HH:mm:ss') + ' [httpLog] ' + JSON.stringify(log) + '\r\n';
    fs.appendFileSync(baseDir + '/http_log.log', data);
    // console.log(log);
  };
};
